import qs from 'qs';
import axios from 'axios';
import conf from './config';
const CancelToken = axios.CancelToken;
let Inc = (new Date()).getTime();
export default {
  metaSources: [],
  _XMLParser(data) {
    let xml, tmp;
    try {
      if (window.DOMParser) { // Standard
        tmp = new window.DOMParser();
        xml = tmp.parseFromString(data, 'text/xml');
      } else { // IE
        xml = new window.ActiveXObject('Microsoft.XMLDOM');
        xml.async = 'false';
        xml.loadXML(data);
      }
    } catch (e) {
      xml = null;
    }
    return xml;
  },
  GetPicGXml(id) {
    return axios.request({
      url: `${conf.base_url}${conf.pic_g_url}${id}.g`,
      // url: `${conf.base_url}xmlFile/${id}.xml`,
      method: 'get',
      responseType: 'text',
      params: { _: Inc++ },
      paramsSerializer: (params) => {
        return qs.stringify(params, { arrayFormat: 'brackets' });
      },
      transformResponse: [(data) => {
        return this._XMLParser(data);
      }]
    });
  },
  GetMetaGXml(fileName) {
    const _self = this;
    return axios.request({
      url: conf.base_url + conf.meta_g_url + fileName,
      method: 'get',
      responseType: 'text',
      params: {},
      paramsSerializer: (params) => {
        return qs.stringify(params, { arrayFormat: 'brackets' });
      },
      transformResponse: [(data) => {
        return this._XMLParser(data);
      }],
      cancelToken: new CancelToken((c) => {
        _self.metaSources.push(c);
      }),
      timeout: 60000
    });
  },
  // 一次图上实时数据
  GetRenderData(params) {
    return axios.post('/jypw/monitor/totalCurrentData', params);
  },
  // 弹框设备属性
  GetDeviceData(params) {
    return axios.get('/rest/rdetail/rlayout', { params });
  },
  // 弹框设备实时数据
  GetDeviceRes(params) {
    return axios.get('/rest/rdetail/resdata', { params });
  },
  // 弹框控制状态
  GetControlStatus(params) {
    return axios.get('/rest/remoteControl/getYkStatus', { params });
  },
  // 发送遥控指令
  SendControlConmand(params) {
    return axios.get('/rest/remoteControl/sendYkConmand', { params });
  },
  // 弹框设备量测值列表
  GetResStdcod(params) {
    return axios.get('/rest/rdetail/resStdcod', { params });
  },
  // 弹框历史曲线
  GetResHistory(params) {
    return axios.get('/rest/rdetail/resHistory', { params });
  },
  // 调压分析弹框
  GetTiaoyaAnalysis(params) {
    return axios.get('/rest/rdetail/tiaoYaAnalysis', { params });
  },
  GetTiaoyaReport(params) {
    return axios.get('/rest/rdetail/tiaoYaReport', { params });
  }
};
